import {
  applyDecorators,
  createParamDecorator,
  ExecutionContext,
  Get,
  SetMetadata,
  UseGuards,
} from '@nestjs/common';
import { Request } from 'express';

export const CustomDecorator = (...args: string[]) =>
  SetMetadata('custom-decorator', args);

export function MergeDecorator(role, guard) {
  return applyDecorators(Get(), CustomDecorator(role), UseGuards(guard));
}

export const MyHeaders = createParamDecorator(
  (key: string, ctx: ExecutionContext) => {
    const request: Request = ctx.switchToHttp().getRequest();
    return key ? request.headers[key.toLowerCase()] : request.headers;
  },
);

export const MyQuery = createParamDecorator(
  (key: string, ctx: ExecutionContext) => {
    const request: Request = ctx.switchToHttp().getRequest();
    return request.query[key];
  },
);
